from BaseDriver import BaseDriver

from hashlib import md5


def encode_password(password):
    salt_head = 'Common Tour'
    salt_end = 'I want an air conditioner during my tour.'
    m = md5()
    m.update(salt_head + password + salt_end)
    return m.hexdigest()


class ReserveDriver(BaseDriver):
    def __init__(self):
        super(ReserveDriver, self).__init__()
        self.user_id = None

    def sign_in(self, username, password):
        secret_password = encode_password(password)
        values = {
            'username': username,
            'password': secret_password
        }
        response = self.doRequest('/sign-in/', values)
        if response['result'] == '_invalid_password':
            print('sign in failed')
        else:
            self.user_id = response['result']
            print('sign in success')

    def sign_up(self, username, password, email):
        secret_password = encode_password(password)
        values = {
            'username': username,
            'password': secret_password,
            'email': email
        }
        response = self.doRequest('/sign-up/', values)
        if response['result'] == '_existed':
            print('sign up failed: user existed')
        else:
            print('sign up success')

    def reset_password(self, username, old_password, new_password):
        secret_old_password = encode_password(old_password)
        secret_new_password = encode_password(new_password)
        values = {
            'username': username,
            'old_password': secret_old_password,
            'new_password': secret_new_password
        }
        response = self.doRequest('/reset-password/', values)
        if response['result'] == '_invalid_password':
            print('reset failed')
        elif response['result'] == 'ok':
            print('reset success')

    def get_service_reserve(self, service_type, service_id):
        values = {
            'service_type': service_type,
            'service_id': service_id
        }
        response = self.doRequest('/get-service-reserve/', values)
        if response['result'] == 'ok':
            reserve = response['reserve']
            volume = response['volume']
            return [reserve, volume]
        else:
            return response['result']

    def get_user_reserve(self):
        if self.user_id is None:
            return '_has_not_signed_in'
        values = {
            'user_id': self.user_id
        }
        result = self.doRequest('/get-user-reserve/', values)
        return result

    def make_reserve(self, service_type, service_id):
        if self.user_id is None:
            return '_has_not_signed_in'
        values = {
            'user_id': self.user_id,
            'service_type': service_type,
            'service_id': service_id
        }
        result = self.doRequest('/make-reserve/', values)
        return result

    def cancel_reserve(self, service_type, service_id):
        if self.user_id is None:
            return '_has_not_signed_in'
        values = {
            'user_id': self.user_id,
            'service_type': service_type,
            'service_id': service_id
        }
        result = self.doRequest('/cancel-reserve/', values)
        return result

reserve_driver = ReserveDriver()
